<?
/****************************************/
/************* IdeaLAN LLC **************/
/* Customer module where you can manage */
/* your customers and devices           */
/*                                      */
/****************************************/
/****************************************/
/* SCRIPT: customers.php                */
/* AUTHOR: Lukasz Piekarski             */
/* E-mail: lukasz@idealan.net           */
if (file_exists("customers.php")) {	$mod_lud = filemtime("customers.php"); }		
$mod_ver	=	"1.5b";
/****************************************/
/*
CUSTOMERS	+
			-
END CUSTOMERS
*/


/* ----------- INSTALLATION ----------- */

if ($_GET["view"] == "install") {

        $config_path = "../../../";
        $cFile = $config_path . "addons/config.ini";
        include ($config_path . "config.php");

	$check_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` = 'Customers' AND `mod_location` = 'customers.php' AND `mod_ver` = '$mod_ver'";  
	$exexute_check_module = mysql_query($check_module); 
	
	if ($exexute_check_module != "") {
		if (mysql_num_rows($exexute_check_module) == 0) { 
			
			
			/* *** CREATE CUSTOMERS TABLE *** */
			$table_customers = "CREATE TABLE `".$DBprefix."customers` (
  			cust_id smallint(5) NOT NULL auto_increment,
  			cust_cname varchar(100) NOT NULL,
  			cust_fname varchar(50) NOT NULL,
  			cust_lname varchar(50) NOT NULL,
  			cust_1phone varchar(15) NOT NULL,
  			cust_2phone varchar(15) NOT NULL,
  			cust_www varchar(50) NOT NULL,
  			cust_email varchar(50) NOT NULL,
  			cust_ddns varchar(50) NOT NULL,
			cust_avk varchar(100) NOT NULL,
  			cust_apt varchar(10) NOT NULL,
  			cust_street varchar(50) NOT NULL,
  			cust_city varchar(50) NOT NULL,
  			cust_state varchar(10) NOT NULL,
  			cust_zip varchar(15) NOT NULL,
  			cust_comments varchar(200) NOT NULL,
  			cust_addby varchar(10) NOT NULL,
  			cust_addate varchar(50) NOT NULL,
  			cust_accnumber varchar(50) NOT NULL default '1',
  			PRIMARY KEY  (cust_id))";

			/* ---------- LET'S CREATE THE TABLE ---------- */
			$create = mysql_query($table_customers);
	
			/* LET'S DISPLAY RESULTS FROM CREATING TABLE */
			if ($create != 1) { echo "CREATING TABLE: <i><b>CUSTOMERS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}
			
			
			/* *** CREATE DEVICES TABLE *** */
			$table_devices = "CREATE TABLE `".$DBprefix."devices` (
  			dev_id smallint(5) NOT NULL auto_increment,
  			dev_custid smallint(5) NOT NULL,
  			dev_type varchar(50) NOT NULL,
  			dev_make varchar(50) NOT NULL,
  			dev_model varchar(50) NOT NULL,
  			dev_os varchar(10) NOT NULL ,
  			dev_comments varchar(225) NOT NULL,
  			dev_adddate varchar(50) NOT NULL,
  			dev_addby varchar(10) NOT NULL,
  			PRIMARY KEY  (dev_id))";

			/* ---------- LET'S CREATE THE TABLE ---------- */
			$create = mysql_query($table_devices);
	
			/* LET'S DISPLAY RESULTS FROM CREATING TABLE */
			if ($create != 1) { echo "CREATING TABLE: <i><b>DEVICES</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}

 
			/* ---------- ADD MODULE TO MODULES LIST ---------- */			
			// LETS CHECK IF THERE ARE ANY MODULES INSTALLED IF NOT LETS MAKE THIS ONE #1
			$get_modules = "SELECT * FROM `".$DBprefix."modules` ORDER BY `mod_id` DESC";  
			$execute_get_modules = mysql_query($get_modules); 
			
			$row_count = mysql_num_rows($execute_get_modules);
			
			if ($row_count != 0) {
			
			$add_module = "INSERT INTO `".$DBprefix."modules` (`mod_id`,`mod_name`,`mod_location`,`mod_ver`,`mod_lud`) VALUES ('$row_count' + 1,'Customers','customers.php','$mod_ver','$mod_lud')";
			$add_result = mysql_query($add_module);
			
			} else {

			$add_module = "INSERT INTO `".$DBprefix."modules` (`mod_id`,`mod_name`,`mod_location`,`mod_ver`,`mod_lud`) VALUES ('1','Customers','customers.php','$mod_ver','$mod_lud')";
			$add_result = mysql_query($add_module);
			
			}

				
			header("Location: ../../in.php?show=config&view=modules&m=20");
			die();
				
		} else {
			echo "ADDING MODULE: <i><b>CUSTOMERS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> Module is already added.<br />";
			
			echo "<a href='../../in.php?show=config&view=modules'>Go Back</a>";
			
			die(); 
		} // END COUNT ROWS 
	} 
} // END INSTALL





/* -------- END INSTALLATION --------- */

/* ----------- REMOVAL ----------- */

if ($_GET["view"] == "remove") {

        $config_path = "../../../";
        $cFile = $config_path . "addons/config.ini";
        include ($config_path . "config.php");


	$check_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` = 'Customers' AND `mod_location` = 'customers.php'";  
	$exexute_check_module = mysql_query($check_module); 
	
	if ($exexute_check_module != "") {
		if (mysql_num_rows($exexute_check_module) > 0) { 
			
			
			/* *** DROP CUSTOMERS TABLE *** */
			$table_customers = "DROP TABLE `".$DBprefix."customers`";

			/* ---------- LET'S DROP THE TABLE ---------- */
			$drop = mysql_query($table_customers);
	
			/* LET'S DISPLAY RESULTS FROM DROPING TABLE */
			if ($drop != 1) { echo "REMOVING TABLE: <i><b>CUSTOMERS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}
			
			
			/* *** DROP DEVICES TABLE *** */
			$table_devices = "DROP TABLE `".$DBprefix."devices`";

			/* ---------- LET'S DROP THE TABLE ---------- */
			$drop = mysql_query($table_devices);
	
			/* LET'S DISPLAY RESULTS FROM DROPING TABLE */
			if ($drop != 1) { echo "REMOVING TABLE: <i><b>DEVICES</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}

 
			/* ---------- REMOVE MODULE FROM MODULES ---------- */	
			$remove_module = "DELETE FROM `".$DBprefix."modules` WHERE `mod_name` = 'Customers' AND `mod_location` = 'customers.php'";
			$remove_result = mysql_query($remove_module);
			
			
			$get_modules = "SELECT * FROM `".$DBprefix."modules` ORDER BY `mod_id` ASC";  
			$execute_get_modules = mysql_query($get_modules); 
			
			$row_count = mysql_num_rows($execute_get_modules);
			
			while ($module = mysql_fetch_array($execute_get_modules)){
				$i = $i + 1;
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
			
			$update_modules = "UPDATE  `".$DBprefix."modules` SET  `mod_id` = '$i'  WHERE `mod_location` = '$mod_location'";
			$execute_upodate_modules = mysql_query($update_modules);
			
			}
				
			header("Location: ../../in.php?show=config&view=modules&m=21");
			die();
				
		} else {
			echo "REMOVING MODULE: <i><b>CUSTOMERS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> Module is already gone.<br />";
			
			echo "<a href='../../in.php?show=config&view=modules'>Go Back.</a>";
			
			die(); 
		} // END COUNT ROWS 
	} 
} // END REMOVAL


if ($_GET["view"] == "backup") {

        $config_path = "../../../";
        $cFile = $config_path . "addons/config.ini";
        include ($config_path . "config.php");

	$backup_file = "Customers_". date("d-m-Y_H-i-s")  . ".gz";
	$backup = "mysqldump --opt -h $server -u$username -p$password $database $DBprefix"."customers $DBprefix"."devices | gzip > $backup_file";
	system($backup);
	
	
	header("Location: ../../in.php?show=config&view=modules&m=22");
	die();	
	
} // END BACKUP

if ($_GET["view"] == "restore") {

        $config_path = "../../../";
        $cFile = $config_path . "addons/config.ini";
        include ($config_path . "config.php");
	
	$restore_file = $_GET["file"];
	$restore = "zcat $restore_file | mysql -u$username -p$password $database";
	system($restore);
	
	
	header("Location: ../../in.php?show=config&view=modules&m=25");
	die();	
	
} // END RESTORE


/* --------- CHANGE ORDER ------------ */
if ($_GET["view"] == "order") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	
	if ($_GET["change"] == "down") {
	
		$new_order = $_GET["modid"] + 1;
		
		$get_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_id` = '$new_order'";  
		$execute_get_module = mysql_query($get_module); 
			
			if ($module = mysql_fetch_array($execute_get_module)){
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
				$update_order2 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '". $_GET["modid"] ."' WHERE `mod_name` = '$mod_name' AND `mod_location` =  '$mod_location' LIMIT 1";
				$update_result = mysql_query($update_order2);
		
			}
			
		$update_order1 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '$new_order' WHERE `mod_name` = 'Customers' AND `mod_location` =  'customers.php' LIMIT 1";
		$update_result = mysql_query($update_order1);
	
	
		header("Location: ../../in.php?show=config&view=modules&m=31");
		die();
	}

	if ($_GET["change"] == "up") {

		$new_order = $_GET["modid"] - 1;
		
		$get_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_id` = '$new_order'";  
		$execute_get_module = mysql_query($get_module); 
			
			if ($module = mysql_fetch_array($execute_get_module)){
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
				$update_order2 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '". $_GET["modid"] ."' WHERE `mod_name` = '$mod_name' AND `mod_location` =  '$mod_location' LIMIT 1";
				$update_result = mysql_query($update_order2);
		
			}
			
		$update_order1 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '$new_order' WHERE `mod_name` = 'Customers' AND `mod_location` =  'customers.php' LIMIT 1";
		$update_result = mysql_query($update_order1);
	
	
		header("Location: ../../in.php?show=config&view=modules&m=32");
		die();
	
	}



} // END CHANGE ORDER

/* --------- UPDATE VERSION ------------ */

if ($_GET["view"] == "verup") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	

	$update_modules = "UPDATE  `".$DBprefix."customers` SET  `mod_ver` = '$mod_ver',`mod_lud` = '$mod_lud' WHERE `mod_location` = 'customers.php'";
	$execute_upodate_modules = mysql_query($update_modules);
	
			
	header("Location: ../../in.php?show=config&view=modules&m=33");
	die();
			
} // END UPDATE VERSION

/* -------- END INSTALATION --------- */


if (empty($_SESSION["sesid"])) { header ("Location: ../../in.php?e=0"); }
?>

		<div id="content">
					<div id="left">
							<div class="naglowek"><div class="pkt"></div>Customers</div>
							<ul id="me">
									<li <? if ($view == "") { echo "id=\"press\""; } ?> ><a href="in.php?show=mod&modid=<? echo $modid; ?>">Customer List</a></li>
									<li <? if ($view == "system") { echo "id=\"press\" "; } ?> ><a href="in.php?show=mod&view=add&modid=<? echo $modid; ?>">Add Customer</a></li>
							</ul>
					
					
					</div>					
					
					<div id="right">

<?

if (!$view) {

// ATTACHE THIS TO THE PAGE, YOU WANT THE NEXT LINK AND PREVIOUS TO BE DISPLAYED
$result = mysql_query("SELECT * FROM `".$DBprefix."customers` ORDER BY `cust_id` DESC"); 
$rownumbers = mysql_num_rows($result);
$linktest ="in.php?show=mod&modid=$modid";


include "inc/pages.php";


// LETS GET CUSTOMERS FROM DATABASE

if ($sq == "") {
	$get_customers = "SELECT * FROM `".$DBprefix."customers` ORDER BY `cust_id` DESC LIMIT $begin,20";
} else {
	$get_customers = "SELECT * FROM `".$DBprefix."customers` WHERE `cust_fname` LIKE '%$sq%' OR `cust_lname` LIKE '%$sq%' OR `cust_1phone` LIKE '%$sq%' OR `cust_email` LIKE '%$sq%' OR `cust_accnumber` LIKE '%$sq%'ORDER BY `cust_id` DESC LIMIT $begin,20"; 
}

$execute_get_customers = mysql_query($get_customers); 
$count_customers = mysql_num_rows($execute_get_customers);

echo "
						<div class='naglowek'><div class='pkt'></div>CUSTOMER LIST</div>
							<div id='rightcont'>
<form method='POST' action='in.php?show=mod&modid=$modid'>

<center><lable>SEARCH: </lable><input name=\"sq\" type=\"text\" class=\"input_form1\"> <input type=\"submit\" value=\"SEARCH\" class=\"button1\"></center><br />

</form>
								<table class='editform1' style='width: 100%'>
       	 <tr>
	  		<td width='50' class='tableLogsHeader'>ID</td>
			<td width='200' class='tableLogsHeader'>NAME</td>
  			<td width='150' class='tableLogsHeader'>PRIMARY PHONE</td>
  			<td width='150' class='tableLogsHeader'>EMAIL</td>
  			<td width='100' class='tableLogsHeader'>CITY</td>
			<td width='100' class='tableLogsHeader'>ADD DATE</td>
			<td width='100' class='tableLogsHeader'>OPTIONS</td>
		</tr>
";

while ($customer = mysql_fetch_array($execute_get_customers)){
$cust_id = $customer["cust_id"];
$cust_cname = $customer["cust_cname"];
$cust_fname = $customer["cust_fname"];
$cust_lname = $customer["cust_lname"];
$cust_1phone = format_phone($customer["cust_1phone"]);
$cust_2phone = $customer["cust_2phone"];
$cust_email = $customer["cust_email"];
$cust_apt = $customer["cust_apt"];
$cust_street = $customer["cust_street"];
$cust_city = $customer["cust_city"];
$cust_state = $customer["cust_state"];
$cust_zip = $customer["cust_zip"];
$cust_comments = $customer["cust_comments"];
$cust_addby = $customer["cust_addby"];
$cust_addate = $customer["cust_addate"];
$cust_accnumber = $customer["cust_accnumber"];

$cid = $cust_id;


$check_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` = 'Projects' AND `mod_location` = 'projects.php'";  
$exexute_check_module = mysql_query($check_module); 
	
if (mysql_num_rows($exexute_check_module) != 0) { 
$extra_links = "<img src='../g/ico/projects.png'>";
}




echo "


       	 <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">
	  		<td class='tableLogs'>&nbsp;$cust_id</td>
	  		<td class='tableLogs'>&nbsp;$cust_lname $cust_fname</td>
  			<td class='tableLogs'>&nbsp;$cust_1phone</td>
  			<td class='tableLogs'>&nbsp;$cust_email</td>
  			<td class='tableLogs'>&nbsp;$cust_city</td>
			<td class='tableLogs'>&nbsp; ". date("m-d-y", $cust_addate) ." </td>
			<td class='tableLogs'>$extra_links <a href='?show=mod&view=detail&modid=$modid&cid=$cid' onmouseover=\"Tip('User Details')\"><img src='../g/ico/user_gray.png'></a> <a href='?show=mod&view=edit&modid=$modid&cid=$cid' onmouseover=\"Tip('Edit User')\"><img src='../g/ico/user_edit.png'></a> <a href=\"javascript:confirmDelete('modify.php?act=delete&what=customer&cid=$cust_id&modid=$modid','DELETING: $cust_lname $cust_fname')\" onmouseover=\"Tip('Delete User')\"><img src='../g/ico/user_delete.png'></a></td>
		</tr>
";
}


echo "
</table><br />
<center>$link_prev $pagejumpline $link_next</center>
							</div>
";


}

if ($view == "add") {	
	// ADD CUSTOMER

// LETS CALCULATE THE ACCOUNT NUMBER
$get_last_accnumber = "SELECT `cust_accnumber` FROM `".$DBprefix."customers` ORDER BY `cust_id` DESC LIMIT 0 , 1";
$execute_get_last_accnumber = mysql_query($get_last_accnumber); 

if ($accnumber = mysql_fetch_array($execute_get_last_accnumber)){
	$last_accnumber = $accnumber["cust_accnumber"];
	$acc_number = substr("$last_accnumber", -4);
	$acc_number = $acc_number + 1;

	if ($acc_number < 10) { $acc_number = "000$acc_number"; } else if ($acc_number < 100) { $acc_number = "00$acc_number"; } else if ($acc_number < 1000) { $acc_number = "0$acc_number"; }

	$acc_number = date("Ym", $timedate) . $acc_number;
} else {

	$acc_number = 1;

	if ($acc_number < 10) { $acc_number = "000$acc_number"; } else if ($acc_number < 100) { $acc_number = "00$acc_number"; } else if ($acc_number < 1000) { $acc_number = "0$acc_number"; }

	$acc_number = date("Ym", $timedate) . $acc_number;
	
}
// END LETS CALCULATE THE ACCOUNT NUMBER

	
echo "
							<div class='naglowek'><div class='pkt'></div>ADD A CUSTOMER</div>
							<div id='rightcont'>
<div class='form_title'>NEW CUSTOMER</div>

<form method='POST' action='modify.php?act=save' class='form_service'>
<input type='hidden' name='what' value='customers'>
<input type='hidden' name='cat' value='addcust'>
<input type='hidden' name='modid' value='$modid'>

	&nbsp;<u>General Information</u><br />


		<label for='cust_fname'>Company Name:&nbsp;</label>
		<input name=\"cust_cname\" type=\"text\" class=\"input_form2\" id=\"cust_cname\" value=\"\"><br />
		<label for='cust_fname'>First Name:&nbsp;</label>
		<input name=\"cust_fname\" type=\"text\" class=\"input_form2\" id=\"cust_fname\" value=\"\"><br />
		<label for='cust_lname'>Last Name:&nbsp;</label>
		<input name=\"cust_lname\" type=\"text\" class=\"input_form2\" id=\"cust_lname\" value=\"\"><br />
		<label for='cust_1phone'>Primary Phone:&nbsp;</label>
		<input name=\"cust_1phone\" type=\"text\" class=\"input_form2\" id=\"cust_1phone\" value=\"\"><br />
		<label for='cust_2phone'>Cell Phone:&nbsp;</label>
		<input name=\"cust_2phone\" type=\"text\" class=\"input_form2\" id=\"cust_2phone\" value=\"\"><br />
		<label for='cust_email'>E-mail:&nbsp;</label>
		<input name=\"cust_email\" type=\"text\" class=\"input_form2\" id=\"cust_email\" value=\"\">

	<br /><br />&nbsp;<u>Location</u><br />
		
		<label for='cust_apt'>Apartment:&nbsp;</label>
		<input name=\"cust_apt\" type=\"text\" class=\"input_form2\" id=\"cust_apt\" value=\"\"><br />
		<label for='cust_street'>Street:&nbsp;</label>
		<input name=\"cust_street\" type=\"text\" class=\"input_form2\" id=\"cust_street\" value=\"\"><br />
		<label for='cust_city'>City:&nbsp;</label>
		<input name=\"cust_city\" type=\"text\" class=\"input_form2\" id=\"cust_city\" value=\"\"><br />
		<label for='cust_state'>State:&nbsp;</label>
		<input name=\"cust_state\" type=\"text\" class=\"input_form2\" id=\"cust_state\" value=\"CT\"><br />
		<label for='cust_zip'>Zip Code:&nbsp;</label>
		<input name=\"cust_zip\" type=\"text\" class=\"input_form2\" id=\"cust_zip\" value=\"\">

	<br /><br />&nbsp;<u>Other</u><br />
	
		<label for='cust_email'>Anti-Virus Key:&nbsp;</label>
		<input name=\"cust_www\" type=\"text\" class=\"input_form2\" id=\"cust_avk\" value=\"\">
		<label for='cust_email'>Website:&nbsp;</label>
		<input name=\"cust_www\" type=\"text\" class=\"input_form2\" id=\"cust_www\" value=\"\">
		<label for='cust_ddns'>DDNS:&nbsp;</label>
		<input name=\"cust_ddns\" type=\"text\" class=\"input_form2\" id=\"cust_ddns\" value=\"\">
		<label for='cust_lname'>Comments:&nbsp;</label>
		<textarea class=\"textarea_form\" name=\"cust_comments\"></textarea><br /><br />
		<label for='cust_accnumber'>Account#:&nbsp;</label>
		<input name=\"cust_accnumber\" type=\"text\" class=\"input_form2\" id=\"cust_accnumber\" value=\"$acc_number\" readonly>


<p>&nbsp;</p>
<p>

    <input type=\"submit\" class=\"button1\" value=\"ADD\">
</form>
</p>
</div>

";
	
	
	
} // END ADD CUSTOMER

if ($view == "edit") {	// EDIT CUSTOMER

// LETS GET CUSTOMER DATA FROM DATABASE
$get_customer = "SELECT * FROM `".$DBprefix."customers` WHERE `cust_id` = '$cid' LIMIT 1";  
$execute_get_customer = mysql_query($get_customer); 

while ($customer = mysql_fetch_array($execute_get_customer)){
$cust_id = $customer["cust_id"];
$cust_cname = $customer["cust_cname"];
$cust_fname = $customer["cust_fname"];
$cust_lname = $customer["cust_lname"];
$cust_1phone = $customer["cust_1phone"]; 
$cust_2phone = $customer["cust_2phone"];
$cust_email = $customer["cust_email"];
$cust_apt = $customer["cust_apt"];
$cust_street = $customer["cust_street"];
$cust_city = $customer["cust_city"];
$cust_state = $customer["cust_state"];
$cust_zip = $customer["cust_zip"];
$cust_www = $customer["cust_www"];
$cust_avk = $customer["cust_avk"];
$cust_ddns = $customer["cust_ddns"];
$cust_comments = $customer["cust_comments"];
$cust_addby = $customer["cust_addby"];
$cust_addate = $customer["cust_addate"];
$cust_accnumber = $customer["cust_accnumber"];
}
	
echo "
							<div class='naglowek'><div class='pkt'></div>EDITING CUSTOMER: $cust_lname $cust_fname</div>
							<div id='rightcont'>
<form method='POST' action='modify.php?act=change'>
<input type='hidden' name='what' value='customers'>
<input type='hidden' name='cat' value='editcust'>
<input type='hidden' name='modid' value='$modid'>
<input type='hidden' name='cid' value='$cid'>
<table style='width: 100%' class='editform1'>
	<tr>
		<td style='width: 145px' class='table1'>&nbsp;</td>
		<td class='table2'>&nbsp;</td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>&nbsp;</td>
		<td class='table3'>&nbsp;General Information</td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Company Name:</td>
		<td class='table3'><input name=\"cust_cname\" type=\"text\" class=\"input_form\" id=\"cust_cname\" value=\"$cust_cname\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>First Name:</td>
		<td class='table3'><input name=\"cust_fname\" type=\"text\" class=\"input_form\" id=\"cust_fname\" value=\"$cust_fname\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Last Name:</td>
		<td class='table2'><input name=\"cust_lname\" type=\"text\" class=\"input_form\" id=\"cust_lname\" value=\"$cust_lname\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Primary Phone</td>
		<td class='table2'><input name=\"cust_1phone\" type=\"text\" class=\"input_form\" id=\"cust_1phone\" value=\"$cust_1phone\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Secondary Phone:</td>
		<td class='table2'><input name=\"cust_2phone\" type=\"text\" class=\"input_form\" id=\"cust_2phone\" value=\"$cust_2phone\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>E-mail:</td>
		<td class='table2'><input name=\"cust_email\" type=\"text\" class=\"input_form\" id=\"cust_email\" value=\"$cust_email\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>&nbsp;</td>
		<td class='table3'>&nbsp;Location</td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Apartment:</td>
		<td class='table2'><input name=\"cust_apt\" type=\"text\" class=\"input_form\" id=\"cust_apt\" value=\"$cust_apt\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Street:</td>
		<td class='table2'><input name=\"cust_street\" type=\"text\" class=\"input_form\" id=\"cust_street\" value=\"$cust_street\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>City:</td>
		<td class='table2'><input name=\"cust_city\" type=\"text\" class=\"input_form\" id=\"cust_city\" value=\"$cust_city\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>State:</td>
		<td class='table2'><input name=\"cust_state\" type=\"text\" class=\"input_form\" id=\"cust_state\" value=\"$cust_state\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Zip Code:</td>
		<td class='table2'><input name=\"cust_zip\" type=\"text\" class=\"input_form\" id=\"cust_zip\" value=\"$cust_zip\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>&nbsp;</td>
		<td class='table3'>&nbsp;Other</td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Anti-Virus Key:</td>
		<td class='table2'><input name=\"cust_avk\" type=\"text\" class=\"input_form\" id=\"cust_avk\" value=\"$cust_avk\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Website:</td>
		<td class='table2'><input name=\"cust_www\" type=\"text\" class=\"input_form\" id=\"cust_www\" value=\"$cust_www\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>DDNS:</td>
		<td class='table2'><input name=\"cust_ddns\" type=\"text\" class=\"input_form\" id=\"cust_ddns\" value=\"$cust_ddns\"></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1' valign=\"top\">Comments:</td>
		<td class='table2'><textarea class=\"textarea_form\" name=\"cust_comments\">$cust_comments</textarea></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Account#:</td>
		<td class='table2'><input name=\"cust_accnumber\" type=\"text\" class=\"input_form\" id=\"cust_accnumber\" value=\"$cust_accnumber\" READONLY></td>
	</tr>

	<tr>
		<td style='width: 145px' class='table1'>&nbsp;</td>
		<td class='table2'>&nbsp;</td>
	</tr>

</table>
<p>&nbsp;</p>
<p>

    <input type=\"submit\" class=\"button1\" value=\"SAVE CHANGES\">
</form>
</p>
</div>

";
	
	
	
} // END EDIT CUSTOMER

if ($view == "detail") {	// VIEW CUSTOMER DETAILS

// LETS GET CUSTOMER DATA FROM DATABASE
$get_customer = "SELECT * FROM `".$DBprefix."customers` WHERE `cust_id` = '$cid' LIMIT 1";  
$execute_get_customer = mysql_query($get_customer); 

while ($customer = mysql_fetch_array($execute_get_customer)){
$cust_id = $customer["cust_id"];
$cust_cname = $customer["cust_cname"];
$cust_fname = $customer["cust_fname"];
$cust_lname = $customer["cust_lname"];
$cust_1phone = $customer["cust_1phone"];
$cust_2phone = $customer["cust_2phone"];
$cust_email = $customer["cust_email"];
$cust_apt = $customer["cust_apt"];
$cust_street = $customer["cust_street"];
$cust_city = $customer["cust_city"];
$cust_state = $customer["cust_state"];
$cust_zip = $customer["cust_zip"];
$cust_avk = $customer["cust_avk"];
$cust_www = $customer["cust_www"];
$cust_comments = $customer["cust_comments"];
$cust_addby = $customer["cust_addby"];
$cust_addate = date("d.m.Y",$customer["cust_addate"]);
$cust_accnumber = $customer["cust_accnumber"];

$get_username = mysql_query("SELECT `user_name` FROM `".$DBprefix."signup` WHERE `user_id` = '$cust_addby'");
while ($user = mysql_fetch_array($get_username)){
$cust_addby = strtoupper($user["user_name"]);
}

}
	
echo "
							<div class='naglowek'><div class='pkt'></div>CUSTOMER DETAILS</div>
							<div id='rightcont'>




<table style='width: 100%' class='editform1'>
	<tr>
		<td style='text-align: left' class='table5'>&nbsp;</td>
		<td class='table5' style='text-align: right'>&nbsp;</td>
      		<td class='table5' style='text-align: left'>&nbsp;</td>
        	<td class='table5' style='text-align: left'>&nbsp;</td>
        	<td class='table5' style='text-align: right'><a href='http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=$cust_street,$cust_city,$cust_state' target='_BLANK' onmouseover=\"Tip('Show MAP')\"><img src='../g/ico/map.png'></a> <a href='?show=mod&view=edit&modid=$modid&cid=$cid' onmouseover=\"Tip('Edit User')\"><img src='../g/ico/user_edit.png'></a> <a href=\"javascript:confirmDelete('modify.php?act=delete&what=customer&cid=$cust_id&modid=$modid','DELETING: $cust_lname $cust_fname')\" onmouseover=\"Tip('Delete User')\"><img src='../g/ico/user_delete.png'></a></td>
	</tr>
	<tr>
		<td style='text-align: left' class='table1'>&nbsp;</td>
		<td class='table2' style='text-align: right'>&nbsp;</td>
      		<td class='table3' style='text-align: left'>&nbsp;</td>
        	<td class='table2' style='text-align: left'>&nbsp;</td>
        	<td class='table2'>&nbsp;</td>
	</tr>
	<tr>
		<td width='25%' class='table1' style='text-align: left'>&nbsp;</td>
		<td width='20%' class='table2' style='text-align: right'>&nbsp; </td>
    		<td width='20%' class='table3'>Personal Information</td>
        	<td width='15%' class='table2'>&nbsp;</td>
        	<td width='20%' class='table2'>&nbsp;</td>
	</tr>
	<tr>
		<td style='text-align: left' class='table1'>&nbsp;&nbsp;&nbsp;ACCOUNT #:&nbsp&nbsp $cust_accnumber</td>
		<td class='table2' style='text-align: right'>Company:<br/>Last Name:</td>
		<td class='table4' style='text-align: left'>&nbsp;$cust_cname<br/>&nbsp;$cust_lname</td>
		<td class='table2' style='text-align: right'>First Name:</td>
		<td class='table4'>&nbsp;$cust_fname</td>
	</tr>
	<tr>
		<td style='text-align: left' class='table1'>&nbsp&nbsp&nbsp;&nbsp;&nbsp;ADDED BY:&nbsp&nbsp $cust_addby</td>
		<td class='table2' style='text-align: right'>Primary Phone:</td>
      		<td class='table4' style='text-align: left'>&nbsp;$cust_1phone</td>
        	<td class='table2' style='text-align: right'>Secondary Phone:</td>
        	<td class='table4'>&nbsp;$cust_2phone</td>
	</tr>
	<tr>
		<td style='text-align: left' class='table1'>&nbsp&nbsp&nbsp;&nbsp;&nbsp;ADD DATE:&nbsp&nbsp $cust_addate</td>
		<td class='table2' style='text-align: right'>E-mail:</td>
     		<td class='table4' style='text-align: left'>&nbsp;$cust_email</td>
        	<td class='table2' style='text-align: right'>&nbsp;</td>
        	<td class='table2'>&nbsp;</td>
	</tr>
	<tr>
		<td style='text-align: left' class='table1'>&nbsp;</td>
		<td class='table2' style='text-align: right'>&nbsp;</td>
      		<td class='table3' style='text-align: left'>&nbsp;</td>
        	<td class='table2' style='text-align: left'>&nbsp;</td>
        	<td class='table2'>&nbsp;</td>
	</tr>
	<tr>
		<td style='text-align: left' class='table1'>&nbsp;</td>
		<td class='table2' style='text-align: right'>&nbsp;</td>
      		<td class='table3' style='text-align: left'>Address Information</td>
        	<td class='table2' style='text-align: left'>&nbsp;</td>
        	<td class='table2'>&nbsp;</td>
	</tr>
	<tr>
		<td style='text-align: left' class='table1'>&nbsp;</td>
		<td class='table2' style='text-align: right'>Apartament:</td>
      		<td class='table4' style='text-align: left'>&nbsp;$cust_apt</td>
        	<td class='table2' style='text-align: right'>Street:</td>
        	<td class='table4'>&nbsp;$cust_street</td>
	</tr>
	<tr>
		<td style='text-align: left' class='table1'>&nbsp;</td>
		<td class='table2' style='text-align: right'>City:</td>
      		<td class='table4' style='text-align: left'>&nbsp;$cust_city</td>
        	<td class='table2' style='text-align: right'>State:</td>
        	<td class='table4'>&nbsp;$cust_state</td>
	</tr>
	<tr>
		<td style='text-align: left' class='table1'>&nbsp;</td>
		<td class='table2' style='text-align: right'>Zip Code:</td>
      		<td class='table4' style='text-align: left'>&nbsp;$cust_zip</td>
        	<td class='table2' style='text-align: right'>&nbsp;</td>
        	<td class='table2'>&nbsp;</td>
	</tr>
    
	<tr>
		<td style='text-align: left' class='table1'>&nbsp;</td>
		<td class='table2' style='text-align: right'>&nbsp;</td>
      		<td class='table2' style='text-align: left'>&nbsp;</td>
        	<td class='table2' style='text-align: right' valign='top'>Comments:</td>
        	<td class='table4'>&nbsp;$cust_comments</td>
	</tr>
	<tr>
		<td style='text-align: left' class='table1'>&nbsp;</td>
		<td class='table2' style='text-align: right'>Anti-Virus Key:</td>
      		<td class='table3' style='text-align: left'>&nbsp;$cust_avk<br/><br/></td>
        	<td class='table2' style='text-align: left'>&nbsp;</td>
        	<td class='table2'>&nbsp;</td>
	</tr>


</table>";

	// LETS GET MODULES THAT ARE IN THE SYSTEM
	$get_modules = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` LIKE 'Services'";  
	$execute_get_modules = mysql_query($get_modules); 

	if ($module = mysql_fetch_array($execute_get_modules)){
		$mod_id = $module["mod_id"];
		$mod_name = $module["mod_name"];
		$mod_location = $module["mod_location"];

	} 

if ($mod_name == "Services") {

// LETS GET SERVICE ORDER DATA
$get_so = "SELECT * FROM `".$DBprefix."service_orders` WHERE `so_cid` = '$cust_id'";  
$execute_get_so = mysql_query($get_so); 

$count_so = mysql_num_rows($execute_get_so);

if (!$count_so == 0) {
echo "
<p>&nbsp;</p>

	<table class='editform1' style='width: 100%'>
       	 <tr>
	  		<td width='50' class='tableLogsHeader'>INV #</td>
			<td width='200' class='tableLogsHeader'>CUSTOMER</td>
  			<td width='125' class='tableLogsHeader'>PHONE</td>
  			<td width='100' class='tableLogsHeader'>STATUS</td>
  			<td width='125' class='tableLogsHeader'>OPEN DATE</td>
			<td width='100' class='tableLogsHeader'>DUE DATE</td>
			<td width='100' class='tableLogsHeader'>TOTAL DUE</td>
			<td width='150' class='tableLogsHeader'>OPTIONS</td>
		</tr>



";



while ($so = mysql_fetch_array($execute_get_so)){
	$so_id = $so["so_id"];
	$so_number = $so["so_number"];
	$so_cid = $so["so_cid"];
	$so_did = $so["so_did"];
	$so_total = $so["so_total"];
	$so_discount = $so["so_discount"];
	$so_tax = $so["so_tax"];
	$so_status = $so["so_status"];
	$so_status_reason = $so["so_status_reason"];
	$so_opendate = date("m.d.y", $so["so_opendate"]);
	$so_paydate = date("m.d.y", $so["so_paydate"]);
	$so_closedate = $so["so_closedate"];
	$so_refunddate = $so["so_refunddate"];
	$so_addby = $so["so_addby"];
	
///////////////////////////////////////////////

// WE NEED TO RESET THE VARIABLES TO 0

	$so_total = "";
	$item_subtotal = "";
	$item_discount = "";
	$item_total = "";
	$so_shTotal = "";
	$so_subTotal = "";
	$so_discTotal = "";

// LETS GET SO DETAILS
$get_sod = "SELECT * FROM `".$DBprefix."service_details` WHERE `sod_soid` = '$so_id'";  
$execute_get_sod = mysql_query($get_sod); 

while ($sod = mysql_fetch_array($execute_get_sod)){
	
	
	$sod_servprice = $sod["sod_servprice"];
	$sod_discount = $sod["sod_discount"];
	$sod_shipping = $sod["sod_shipping"];
	$sod_tax = $sod["sod_tax"];
	$sod_taxtotal = $sod["sod_taxtotal"];
	$sod_qty = $sod["sod_qty"];
	$sod_total = $sod["sod_total"];
	

	$item_subtotal = $sod_servprice * $sod_qty;
	$item_discount = ($sod_discount / 100) * $item_subtotal;
	
	$item_total = $item_subtotal - $item_discount;
	
	$so_shTotal = $so_shTotal + $sod_shipping;
	$so_subTotal = $so_subTotal + $item_total;

$so_discTotal = ($so_discount / 100) * $so_subTotal;

if ($so_tax == 1) { $so_taxTotal = $so_subTotal * 0.06;}

$so_total = $so_subTotal + $so_shTotal + $so_taxTotal - $so_discTotal;


}
	if (empty($so_total)) { $so_total= "0"; }

///////////////////////////////////////////////



	// LETS GET CUSTOMER DATA FROM DATABASE
	$get_customer = "SELECT * FROM `".$DBprefix."customers` WHERE `cust_id` = '$so_cid' LIMIT 1";  
	$execute_get_customer = mysql_query($get_customer); 

	if ($customer = mysql_fetch_array($execute_get_customer)){
	$cust_id = $customer["cust_id"];
	$cust_fname = $customer["cust_fname"];
	$cust_lname = $customer["cust_lname"];
	$cust_1phone = $customer["cust_1phone"];
	$cust_city = $customer["cust_city"];
	$cust_state = $customer["cust_state"];
	
	if ($so_status <= 0) { $so_status = "UNPAID"; $so_status = "UNPAID"; $mark = "red"; $del_link = "<a href=\"javascript:confirmDelete('modify.php?act=delete&what=services&cat=so&soid=$so_id','DELETING INVOICE: $so_number')\" onmouseover=\"Tip('Delete User')\"><img src='../g/ico/so_delete.png'></a>"; $pay_link = "<a href=\"javascript:confirmDelete('modify.php?act=change&what=services&cat=so&soid=$so_id&status=1','MARK AS PAID: $so_number')\" onmouseover=\"Tip('Mark As PAID')\"><img src='../g/ico/money_add.png'></a>"; } else if ($so_status == 1) { $so_status = "PAID"; $mark = "green";} else if ($so_status == 2) { $so_status = "REFUND"; $mark = "yellow"; $so_total = "-$so_total"; } else if ($so_status == 3) { $so_status = "CANCELED"; $so_total = "0"; $mark = "red";} else { $so_status = "UNPAID"; }


	echo "

       	 <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">
	  		<td class='tableLogs'>$so_number</td>
	  		<td class='tableLogs'>$cust_lname $cust_fname</td>
  			<td class='tableLogs'>$cust_1phone</td>
  			<td class='tableLogs$mark'>&nbsp;$so_status</td>
  			<td class='tableLogs'>$so_opendate</td>
  			<td class='tableLogs'>$so_paydate</td>
			<td class='tableLogs'>$" .number_format($so_total, 2, '.', ','). "</td>
			<td class='tableLogs'>$extra_links <A HREF=\"javascript:popUp('print.php?soid=$so_id&to=F')\" onmouseover=\"Tip('Email Inv.')\"><img src='../g/ico/so_email.png'> </A> <A HREF=\"print.php?soid=$so_id&to=I\" target='_blank' onmouseover=\"Tip('Display Inv.')\"><img src='../g/ico/so_print.png'> </A><a href='?show=mod&view=detail&soid=$so_id&modid=$mod_id' onmouseover=\"Tip('Edit Inv.')\"><img src='../g/ico/so.png'> </a> $pay_link $del_link</td>
		</tr>

	";

} // END GET CUSTOMER DATA
	

} // END GET SO DATA
} // END COUNT ROWS

} // END CHECK IF SERVICES EXISTS

echo "
<table style='width: 100%' class='editform1'>
	<tr>
		<td width='15%' class='table5' style='text-align: center'>TYPE</td>
		<td width='10%' class='table5' style='text-align: center'>MAKE</td>
		<td width='20%' class='table5' style='text-align: center'>MODEL</td>
		<td width='20%' class='table5' style='text-align: center'>OS</td>
		<td width='30%' class='table5'>COMMENTS</td><br />
		<td width='15%' class='table5'>OPTIONS</td>
	</tr>
";


// LETS GET CUSTOMER DEVICES FROM DATABASE
$get_devices = "SELECT * FROM `".$DBprefix."devices` WHERE `dev_custid` = '$cid'";  
$execute_get_devices = mysql_query($get_devices); 

while ($device = mysql_fetch_array($execute_get_devices)){

$dev_id = $device["dev_id"];
$cust_id = $device["dev_custid"];
$dev_type = $device["dev_type"];
$dev_make = $device["dev_make"];
$dev_model = $device["dev_model"];
$dev_os = $device["dev_os"];
$dev_comments = $device["dev_comments"];
$dev_adddate = $device["dev_adddate"];
$dev_addby = $device["dev_addby"];

echo "
	<tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">
		<td class='table6'>$dev_type</td>
		<td class='table6'>$dev_make</td>
		<td class='table6'>$dev_model</td>
		<td class='table6'>$dev_os</td>
		<td class='table6'>&nbsp;$dev_comments</td>
		<td class='table6'><!-- <img src='../g/ico/device_edit.png'> --> &nbsp;<a href='modify.php?act=delete&what=device&modid=$modid&cid=$cust_id&did=$dev_id' onmouseover=\"Tip('Remove Device')\"><img src='../g/ico/device_delete.png'></a></td>
	</tr>
";

}



echo "

</table>
<form method='POST' action='modify.php?act=save'>
<input type='hidden' name='what' value='customers'>
<input type='hidden' name='cat' value='adddevice'>
<input type='hidden' name='modid' value='$modid'>
<input type='hidden' name='cid' value='$cid'>
<table style='width: 100%' class='editform1'>
	<tr>
		<td width='15%' style='text-align: center'><select name=\"dev_type\" class=\"input_form1\"><option>DESKTOP</option><option>NOTEBOOK</option><option>NETBOOK</option><option>SERVER</option></select></td>
		<td width='10%' style='text-align: center'><input name=\"dev_make\" type=\"text\" class=\"input_form1\"></td>
		<td width='20%' style='text-align: center'><input name=\"dev_model\" type=\"text\" class=\"input_form1\"></td>
		<td width='20%' style='text-align: center'><input name=\"dev_os\" type=\"text\" class=\"input_form1\"></td>
		<td width='15%' style='text-align: center'><input name=\"dev_comments\" type=\"text\" class=\"input_form\"></td><br />
		<td width='15%' style='text-align: center'><input type=\"image\" src=\"../g/ico/device_add.png\" onmouseover=\"Tip('Add Device')\" /></td>


	</tr>
</table>
</form>
</div>

";
	
	
	
} // END VIEW DETAIL




?>
</div>
